package com.situ.gym.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.situ.gym.pojo.Permission;
import com.situ.gym.pojo.vo.PermissionVO;
import com.situ.gym.service.IPermissionService;
import com.situ.gym.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gao
 * @since 2025-09-01
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    @GetMapping("/selectPermissionTree")
    public Result<List<PermissionVO>> selectPermissionTree() {
        List<PermissionVO> list = permissionService.selectPermissionTree();
        return Result.ok(list);
    }

    // /deleteById?id=1
    // /deleteById/1
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        permissionService.removeById(id);
        return Result.ok("删除成功");
    }

    // @RequestBody : 封装前台传递过来的JSON格式数据
    // @ResponseBody : 将数据转换为JSON格式返回
    @PostMapping("/add")
    public Result add(@RequestBody Permission permission) {
        System.out.println(permission);
        permissionService.save(permission);
        return Result.ok("添加成功");
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Permission permission = permissionService.getById(id);
        return Result.ok(permission);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Permission permission) {
        System.out.println(permission);
        //permissionService.saveOrUpdate(permission);
        //permissionService.updateById(permission);
        permissionService.update(permission);
        return Result.ok("更新成功");
    }
}

